package com.hss.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class StudentController {

    @Autowired
    IUser userMaper;

    @RequestMapping(value = "/toAddStudent", method = RequestMethod.GET)
    public String student(ModelMap model) {
//        return new ModelAndView("addStudent", "command", new Student());
        model.addAttribute("student",new Student());

        List<Order> orders = userMaper.getUserOrders(1);
        for (Order order : orders) {
            System.out.println("订单号："+order.getOrderNo() + "，订单金额：" + order.getMoney());
        }
        model.addAttribute("orders",orders);


        return "addStudent";
    }

    @ModelAttribute("student")
    public Student createStudentModel() {
        return new Student();
    }

    @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
    public String addStudent(@ModelAttribute("student") @Validated Student student,
                             BindingResult bindingResult, Model model) {
        if (bindingResult.hasErrors()) {
            return "addStudent";
        }
        model.addAttribute("name", student.getName());
        model.addAttribute("age", student.getAge());
        model.addAttribute("id", student.getId());

        return "result";
    }
}